package com.atguigu.gmall.product.utils;

import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.util.ClassUtils;

import java.io.IOException;

public class FileUploadUtils {
    private static TrackerClient trackerClient = null;

    static {
        try {
            //连接跟踪器和连接存储结点不能抽取  因为不符合原理图的跟踪器和存储结点的交互情况
            ClientGlobal.init("D:\\WLin\\IntelliJ IDEA 2018.3.2\\Project\\gmall_sph\\gmall-parent\\service\\service-product\\src\\main\\resources\\fdfs_client.conf");
            //1:创建客户端
            trackerClient = new TrackerClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //上传
    public static String uploadFile(byte[] file, String ext) {
        try {
            //2:返回值存储节点地址
            TrackerServer trackerServer = trackerClient.getConnection();
            //3:连接存储节点
            StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);
            //4:上传文件
            String fileId = fileId = storageClient1.upload_file1(file, ext, null);
            return fileId;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
